package com.tmall.wireless.tangram.example.card;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.tmall.wireless.tangram.example.R;
import com.tmall.wireless.tangram.structure.BaseCell;
import com.tmall.wireless.tangram.structure.view.ITangramViewLifeCycle;

public class ActivityTwo extends FrameLayout implements ITangramViewLifeCycle {
    private ImageView imageView;
    private BaseCell cell;
    private Context context ;
    public ActivityTwo(Context context) {
        super(context);
        init();
        this.context =context;
    }

    public ActivityTwo(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        this.context =context;
    }

    public ActivityTwo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        this.context =context;
    }

    private void init(){
        inflate(getContext(), R.layout.item_activity_two, this);
        imageView = findViewById(R.id.image);
    }

    @Override
    public void cellInited(BaseCell cell) {
        setOnClickListener(cell);
        this.cell = cell;
    }

    @Override
    public void postBindView(BaseCell cell) {
        displayImageContent1(context,cell.optStringParam("img_url"),imageView);

    }

    @Override
    public void postUnBindView(BaseCell cell) {
    }
    public   void displayImageContent1(final Context context, final Object path, final ImageView imageView) {
        Glide.with(context).asBitmap().load(path.toString()).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                int imageWidth = resource.getWidth();
                int imageHeight = resource.getHeight();
                ViewGroup.LayoutParams para = imageView.getLayoutParams();


                int maxHeight = 3000;
                int height = (int) ((float) imageView.getWidth() / imageWidth * imageHeight);
                if (height > maxHeight) height = maxHeight;
                para.height = height;
                imageView.setLayoutParams(para);
                Glide.with(context).asBitmap().load(path.toString()).into(imageView);
            }

        });

    }
}

